package com.example.task05_servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "LoginServlet", value = "/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.接收前端页面输入的用户名和密码信息并打印
        String username = request.getParameter("username");
        System.out.println("接收到的用户名为:"+username);
        String password = request.getParameter("password");
        System.out.println("接收到的密码为:"+password);
        //2.使用固定的用户名和密码信息来进行登录的校验
        if("scd".equals(username)&&"150611".equals(password)){
            System.out.println("登录成功!");
            response.sendRedirect("main.jsp");
            //存储用户名信息
            request.getSession().setAttribute("username",username);
        }else{
            System.out.println("用户名或密码错误!");
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("login.jsp");
            requestDispatcher.forward(request,response);

        }
    }
}
